<!DOCTYPE HTML>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
    <meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
    <title>登录frm</title>
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <style type="text/css">
    .aui-card {
        margin-top: 15px;
    }
    .aui-input-addon.aui-iconfont {
        font-size: 20px !important;
    }
    .aui-input-row {
        width: 100% !important;
    }
    </style>
</head>
<body>
    <div class="aui-content aui-card">       
        <div class="aui-form">
            <div class="aui-input-row">
                <i class="aui-input-addon aui-iconfont aui-icon-people aui-text-primary"></i>
                <input type="text" id="username" class="aui-input" placeholder="E-Mail"/>
            </div>

            <div class="aui-input-row">
                <i class="aui-input-addon aui-iconfont aui-icon-lock aui-text-primary"></i>
                <input type="password" id="password" class="aui-input" placeholder="密码"/>
                <i class="aui-input-addon aui-iconfont aui-icon-attention aui-text-default" onclick="showPassword();" id="showpass-btn"></i>
            </div>

            <div class="aui-btn-row">
                <div class="aui-btn aui-btn-block aui-btn-primary" tapmode onclick="login()">确认</div>
            </div>
        </div>
    </div>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script type="text/javascript" src="../script/zepto.min.js"></script>
<script type="text/javascript" src="../script/common.js" ></script>
<script type="text/javascript" src="../script/OSChinaApi.js" ></script>
<script type="text/javascript">
    apiready = function () {

    }
    function showPassword(){
        $api.attr($api.byId('password'),'type','text');
        $api.removeCls($api.byId('showpass-btn'),'aui-icon-attention');
        $api.addCls($api.byId('showpass-btn'),'aui-icon-attentionfill');
        $api.attr($api.byId('showpass-btn'),'onclick','hidePassword();');
    }
    function hidePassword(){
        $api.attr($api.byId('password'),'type','password');
        $api.removeCls($api.byId('showpass-btn'),'aui-icon-attentionfill');
        $api.addCls($api.byId('showpass-btn'),'aui-icon-attention');
        $api.attr($api.byId('showpass-btn'),'onclick','showPassword();');
    }
    
    function login(){
    	var username 	= $api.val($api.byId('username'));
    	var pwd 		= $api.val($api.byId('password'));
    	if( isEmail(username) == false ){
			api.toast({
			    msg: 'E-Mail格式不正确',
			    duration:2000,
			    location: 'middle'
			});
			
			return false;
    	}
    	
    	if(pwd.length == 0){
			api.toast({
			    msg: '请输入密码',
			    duration:2000,
			    location: 'middle'
			});
			
			return false;
    	}
    	
		api.showProgress({
		    style: 'default',
		    animationType: 'fade',
		    title: '登录中...',
		    text: '',
		    modal: true
		});
    	
		api.ajax({
		    url: OpenAPI.login,
		    method: 'post',
		    timeout: 30,
		    dataType: 'text',
		    returnAll:false,
		    data:{
		        values: {
		        			keep_login: 1,
		        			username: username,
		        			pwd: pwd,
		        		},
		    }
		},function(ret,err){
			api.hideProgress();
		    if (ret) {
				var trans = api.require('trans');
				trans.parse({
				    data:ret
				},function(ret,err){
				    if(ret) {
				    	if(parseInt(ret.oschina.result.errorCode) == 1){	// 登录成功
				    		$api.setStorage('user_uid', ret.oschina.user.uid);
				    		$api.setStorage('user_favoritecount', ret.oschina.user.favoritecount);
				    		$api.setStorage('user_followers', ret.oschina.user.followers);
				    		$api.setStorage('user_location', ret.oschina.user.location);
				    		$api.setStorage('user_fans', ret.oschina.user.fans);
				    		$api.setStorage('user_name', ret.oschina.user.name);
				    		$api.setStorage('user_score', ret.oschina.user.score);
				    		$api.setStorage('user_gender', ret.oschina.user.gender);
				    		$api.setStorage('user_portrait', ret.oschina.user.portrait);
				    		
							api.execScript({
							    name: 'root',
							    frameName: 'footer_tab_3',
							    script: 'api.refreshHeaderLoading();'
							});
				    		
				    		api.closeWin();
				    		
				    	}else{		// 登录失败
							api.toast({
							    msg: ret.oschina.result.errorMessage,
							    duration:2000,
							    location: 'middle'
							});
				    	}
				    }else{
				        log(err.msg);
				    }
				});
		    }else {
		       log('错误码：'+err.code+'；错误信息：'+err.msg+'网络状态码：'+err.statusCode);
		    }
		});
    }
    
	function isEmail(str){
		var reg = /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-])+/;
		return reg.test(str);
	}   
</script>
</html>